import os
import threading

from entity.other.Entitys import Person
from kitool.DateKit import get_datestr

from kitool.NlpKit import tranfer_person

import threading

from kitool import ConfigKit as conf

from kitool import JsonKit


# 安全线程
lock = threading.Lock()

# 线程方法
def process_data(data, work_dir, nlp_url, out_name, top_icon):
    with lock:
        # 这里是处理数据的具体逻辑，可以根据实际情况修改
        one = Person(data)
        out_txt = tranfer_person(one, nlp_url, top_icon)
        # 打开文件以追加模式写入
        full_out_path = os.path.join(work_dir, get_datestr(),  get_datestr() + '__' + out_name + '__' + (str(int(one.id) % 10)) + '.md')
        with open(full_out_path, 'a', encoding='UTF-8') as file:
            file.write(out_txt + '\n')



def process_data_without_thread(data, work_dir, nlp_url, out_name, top_icon):
    # 这里是处理数据的具体逻辑，可以根据实际情况修改
    one = Person(data)
    out_txt = tranfer_person(one, nlp_url, top_icon)
    # 打开文件以追加模式写入
    full_out_path = os.path.join(work_dir, get_datestr(),
                                 get_datestr() + '__' + out_name + '__' + (str(int(one.id) % 10)) + '.md')
    with open(full_out_path, 'a', encoding='UTF-8') as file:
        file.write(out_txt + '\n')


def run():
    res = conf.readConf('static/markdown.ini')
    md = res['md']
    nlp_url = md['nlp_url']
    work_dir = md['work_dir']
    json_name = md['json_name']
    out_name = md['out_name']
    top_icon = md['top_icon']
    #
    alist = JsonKit.data_list(work_dir, json_name)
    len_list = len(alist)
    for data in alist:
        # t = threading.Thread(target=process_data, args=(data, work_dir, nlp_url, out_name, top_icon))
        # t.start()
        process_data_without_thread(data, work_dir, nlp_url, out_name, top_icon)

        len_list = len_list - 1
        print(len_list)

    #
    print('done!')

